package cn.com.thingo.hyintelligent.ui.activity;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.videogo.errorlayer.ErrorInfo;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.EZPlayer;

import java.lang.ref.WeakReference;


import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.base.BaseActivity;

import cn.com.thingo.hyintelligent.base.contract.EzDeviceFullScreenContract;
import cn.com.thingo.hyintelligent.presenter.EzDeviceFullScreenPresenter;
import cn.com.thingo.hyintelligent.util.SPUtils;
import cn.com.thingo.hyintelligent.widget.PlayerLoadingTextView;


public class EzDeviceFullScreenActivity extends BaseActivity<EzDeviceFullScreenPresenter> implements EzDeviceFullScreenContract.View {
    @BindView(R.id.toolbar_left)
    ImageView ivLeft;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.ll_toolbar)
    LinearLayout llToolbar;
    @BindView(R.id.sv)
    SurfaceView svPlayer;
    @BindView(R.id.realplay_loading)
    PlayerLoadingTextView playerLoadingTextView;
    @BindView(R.id.realplay_play_iv)
    ImageView ivPlay;
    private EZPlayer mEZPlayer;
    private  String mDeviceName;
    private static String mDeviceSerial;
    private static String mVerifyCode;
    private SPUtils mSPUtils;
    private boolean isPlay;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_player_full_screen;
    }

    @Override
    protected void initEventAndData() throws Exception {
        mSPUtils = SPUtils.newInstance();
        mDeviceName = getIntent().getStringExtra("name");
        mDeviceSerial = getIntent().getStringExtra("deviceSerial");
        mVerifyCode = mSPUtils.getString(mDeviceSerial);
        initTitle();
        ivPlay.setVisibility(View.VISIBLE);
        playerLoadingTextView.setVisibility(View.GONE);

    }

    private void initEzPlayer() {
        mEZPlayer = EZOpenSDK.getInstance().createPlayer(mDeviceSerial, 1);
        mEZPlayer.setSurfaceHold(svPlayer.getHolder());
        mEZPlayer.setPlayVerifyCode(mVerifyCode);
        MessageHandler messageHandler = new MessageHandler();
        mEZPlayer.setHandler(messageHandler);
        mEZPlayer.startRealPlay();
        ivPlay.setVisibility(View.GONE);
        playerLoadingTextView.setVisibility(View.VISIBLE);
    }

    class MessageHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_SUCCESS:
                    isPlay = true;
                    SPUtils.newInstance().put(mDeviceSerial, mVerifyCode);
                    ivPlay.setVisibility(View.GONE);
                    playerLoadingTextView.setVisibility(View.GONE);
                    break;
                case EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_FAIL:
                    isPlay = false;
                    ivPlay.setVisibility(View.VISIBLE);
                    playerLoadingTextView.setVisibility(View.GONE);
                    break;
                case EZConstants.EZRealPlayConstants.MSG_REALPLAY_VOICETALK_SUCCESS:
                    break;
                case EZConstants.EZRealPlayConstants.MSG_REALPLAY_VOICETALK_STOP:
                    break;
                case EZConstants.EZRealPlayConstants.MSG_REALPLAY_VOICETALK_FAIL:
                    break;
            }
        }
    }


    private void initTitle() {
        ivLeft.setImageResource(R.mipmap.back);
        llToolbar.setAlpha(0.6f);
        tvTitle.setText(mDeviceName);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                llToolbar.setVisibility(View.GONE);
            }
        }, 2000);
    }

    @OnClick({R.id.toolbar_left, R.id.sv,R.id.realplay_play_iv})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
            case R.id.sv:
                llToolbar.setVisibility(View.VISIBLE);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (llToolbar!=null){
                            llToolbar.setVisibility(View.GONE);
                        }

                    }
                }, 2000);
                break;
            case R.id.realplay_play_iv:
                initEzPlayer();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (mEZPlayer != null && isPlay) {
            ivPlay.setVisibility(View.GONE);
            playerLoadingTextView.setVisibility(View.VISIBLE);
            mEZPlayer.setSurfaceHold(svPlayer.getHolder());
            mEZPlayer.startRealPlay();
        }
    }

    @Override
    protected void onDestroy() {
        if (mEZPlayer != null) {
            mEZPlayer.release();
        }
        super.onDestroy();

    }
}
